package com.xy.controller.uc;


import com.xy.common.base.ApiBaseResponse;
import com.xy.common.base.BaseController;
import com.xy.entity.User;
import com.xy.entity.uc.Address;


import com.xy.repository.AddressRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "地址Controller", description = "地址接口")
@RestController
@RequestMapping(value = "api/address")
public class AddressController extends BaseController {

    @Autowired
    private AddressRepository addressRepository;


    @ApiOperation("查询所有地址")
    @ResponseStatus(HttpStatus.OK)
    @GetMapping(value = "/address")
    @Transactional
    public ResponseEntity getAllAddresses() {
        initBaseInfo();
        List<Address> addresslist = addressRepository.findAllByUserId(userId);
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK, addresslist));
    }

    @ApiOperation("第一次添加地址")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/address")
   /* @ApiImplicitParams({
            @ApiImplicitParam(name ="area",value = "区",required=true),
            @ApiImplicitParam(name ="city",value = "市",required=true),
            //@ApiImplicitParam(name ="defaultAddress",value = "是否为默认地址",required=true),
            @ApiImplicitParam(name ="phone",value = "电话号码",required=true),
            @ApiImplicitParam(name ="postcode",value = "邮编",required=true),
            @ApiImplicitParam(name ="province",value = "省",required=true),
            @ApiImplicitParam(name ="receiver",value = "收件人姓名",required=true),
            @ApiImplicitParam(name ="village",value = "详细地址",required=true)
    })*/
    @Transactional
    public ResponseEntity saveAddress(@RequestBody Address address) {
        initBaseInfo();
        address.setUserId(userId);
        //address.setDefaultAddress("1");
        addressRepository.save(address);
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,address.getUuid()));
    }

    @ApiOperation("删除地址")
    @ResponseStatus(HttpStatus.OK)
    @DeleteMapping(value = "/address/{uuid}")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="uuid",value = "地址id",required=true)
    })
    @Transactional
    public ResponseEntity deleteAddress(@PathVariable(value = "uuid")String uuid) {

        initBaseInfo();
        Address address=addressRepository.findByUuidAndAndUserId(uuid,userId);
        if (!uuid.equals(address.getUuid())) {
            return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.INTERNAL_SERVER_ERROR));
        }
        addressRepository.deleteByUuid(uuid);
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK));
    }


    @ApiOperation("更新地址")
    @ResponseStatus(HttpStatus.OK)
    @PostMapping(value = "/updateAddress")
  /*  @ApiImplicitParams({
            @ApiImplicitParam(name ="uuid",value = "地址id",required=true),
            @ApiImplicitParam(name ="area",value = "区",required=true),
            @ApiImplicitParam(name ="city",value = "市",required=true),
            @ApiImplicitParam(name ="defaultAddress",value = "是否为默认地址",required=true),
            @ApiImplicitParam(name ="phone",value = "电话号码",required=true),
            @ApiImplicitParam(name ="postcode",value = "邮编",required=true),
            @ApiImplicitParam(name ="province",value = "省",required=true),
            @ApiImplicitParam(name ="receiver",value = "收件人姓名",required=true),
            @ApiImplicitParam(name ="village",value = "详细地址",required=true),
            @ApiImplicitParam(name ="user_id",value = "用户id",required=true)
    })*/
    @Transactional
    public ResponseEntity updateAddress(@RequestBody Address address) {

        if (address.getDefaultAddress().equals("1")) {
            List<Address> addresslist = addressRepository.findByDefaultAddress("1");
            if (addresslist.size() > 0) {
                for (Address tempAddress : addresslist) {
                    tempAddress.setDefaultAddress("0");
                }
            }
        }

        Map resultMap = new HashMap<String,String>();

        Address resultAddress = addressRepository.save(address);
        resultMap.put("uuid",resultAddress.getUuid());
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,resultMap));
    }


}






